package org.buptsse.utils;

import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.LinkedList;

import org.buptsse.entity.Post;

import android.R.integer;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class JsonUtils {
	
	public void parsePostFromJson(String jsonData){
		Type listType = new TypeToken<LinkedList<Post>>(){}.getType();
		Gson gson = new Gson();
		LinkedList<Post> posts = gson.fromJson(jsonData, listType);
		for (Iterator iterator = posts.iterator(); iterator.hasNext();) {
			Post post = (Post) iterator.next();
			System.out.println("post Content:--->" + post.getPostContent());
		}
	}
	
	//List<String> testList2 = (List<String>) gson.fromJson(listToJson,new TypeToken<List<String>>() {}.getType());  
	public static Object getJsonObject(String json, Type type)
	{
		Gson gson = new Gson();
		int index1=json.indexOf('[');
		int index2=json.lastIndexOf(']');
		if(index1!=-1 && index2!=-1)
			json=json.substring(index1,index2+1);
		return gson.fromJson(json, type);
	}
}
